from django.contrib.gis import admin
from django.utils.safestring import mark_safe

from transport.models import GPSLog
from transport.models import ErrorReport
from transport.models import Location

REJSEPLANEN_URL = 'http://xmlopen.rejseplanen.dk/bin/rest.exe'

class GPSLogAdmin(admin.ModelAdmin):
  list_display = ('user', 'timestamp', 'map_preview', 'stops_nearby')
  list_filter = ('user',)

  def map_preview(self, entry):
    return mark_safe(
      '<a href="http://maps.google.com/?q=%s,%s">View</a>'% \
      (entry.latitude, entry.longitude))
  map_preview.short_description = "map preview"
  map_preview.allow_tags = True

  def stops_nearby(self, entry):
    return '<a href="%s/stopsNearby?coordX=%s&coordY=%s&maxRadius=1000&maxNumber=30">Stop s.</a>' % \
        (REJSEPLANEN_URL, entry.x, entry.y)
  stops_nearby.allow_tags = True
admin.site.register(GPSLog, GPSLogAdmin)


class ErrorReportAdmin(admin.ModelAdmin):
  list_display = ('user', 'tag', 'created')
  list_filter = ('user', 'tag')

admin.site.register(ErrorReport, ErrorReportAdmin)

class LocationAdmin(admin.ModelAdmin):
  list_display = ('user', 'map_preview', 'count', 'radius', '__unicode__')
  list_filter = ('user', )
  ordering = ('x', 'y')

  def map_preview(self, entry):
    return mark_safe(
      '<a href="http://maps.google.com/?q=%s,%s">View</a>'% \
      #'%s' % \
      (entry.geom.get_y(), entry.geom.get_x())
    )
  map_preview.short_description = "map preview"
  map_preview.allow_tags = True


admin.site.register(Location, LocationAdmin)
#admin.site.register(Location, admin.GeoModelAdmin)
